#include "itemdelegate.h"

ItemDelegate::ItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{

}


ItemDelegate::ItemDelegate(int type, QObject * parent):
    QStyledItemDelegate(parent),
    m_type(type)
{

}


void ItemDelegate::paint(QPainter * painter,
           const QStyleOptionViewItem & option,
           const QModelIndex & index) const
{
    if(0 == m_type) //header tablewidget
    {
//        QColor color;
//        color.setRgb(213, 233, 244);

        //background color not right
//        int startX = option.rect.x();
//        int startY = option.rect.y();
//        int endX = startX;
//        int endY = option.rect.bottom();
//        QLinearGradient linearGrad(startX,startY,endX,endY);
//        linearGrad.setSpread(QGradient::PadSpread);
//        linearGrad.setColorAt(0,QColor(" red"));
//        linearGrad.setColorAt(0.6,QColor("green"));
//        linearGrad.setColorAt(1,QColor("blue"));
//        qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #C3F2F6,stop:0.6 #79C7E7 ,stop:1 #6CACCC);
//        linearGrad.setColorAt(0,QColor("#C3F2F6"));
//        linearGrad.setColorAt(0.6,QColor("#79C7E7"));
//        linearGrad.setColorAt(1,QColor("#6CACCC"));
        //绘制背景
//        painter->setPen(color);
//        painter->setBrush(QBrush(linearGrad));    //not work
//        painter->drawRect(option.rect);

        //字体
        painter->setPen(QColor(0,0, 0));
        QTextOption op;
        op.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        QFont font;
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(16);
//        font.setBold(true);
        painter->setFont(font);

        QRect rect;
        rect = option.rect;
        painter->drawText(rect, index.data(Qt::DisplayRole).toString(), op);
    }
    else {  //content table widget
        //文字
//        painter->setPen(QColor(0, 0, 0));
        QTextOption op;
        op.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

        QFont font;
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(16);
        painter->setFont(font);
        if( option.state & QStyle::State_Selected ) {
            painter->fillRect( option.rect, QBrush(QColor("#E4F6FB")));
        }
        painter->drawText(option.rect, index.data(Qt::DisplayRole).toString(), op);

//        painter->setPen(QColor(248,251,255));
//        painter->setPen(QColor("red"));
//        painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());//底边框
//        painter->drawLine(option.rect.topRight(), option.rect.bottomRight());//右边框
    }
}
